// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: linchuan

#include "coin2/exchange/okex_v5/order_v5/parser.h"

#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "coin2/base/conversion.h"
#include "coin2/base/log.h"
#include "coin2/exchange/okex/symbology/currency.h"
#include "coin2/exchange/okex_v5/api_order/native_private_client.h"
#include "coin2/exchange/okex_v5/order_v5/error_code.h"
#include "coin2/exchange/util/json_util.h"

namespace coin2::exchange::okex_v5::order_v5 {

namespace {

using coin::proto::AccountBalance;
using coin::proto::AccountPosition;
using coin::proto::CancelOrderResponse;
using coin::proto::CurrencyTransferElement;
using coin::proto::FillType;
using coin::proto::ProductFillBundle;
using coin::proto::ProductOrderBundle;
using coin::proto::ProductOrderElement;
using coin::proto::SubmitOrderResponse;
using coin::proto::TransferRequestProto;
using coin2::exchange::base::order::IOrderContext;
using JsonValue = rapidjson::GenericValue<rapidjson::UTF8<>>;
using TransferType = coin::proto::TransferRequestProto::TransferType;
using OkexV5OrderSubTopicId = coin2::exchange::okex_v5::api_order::OkexV5OrderSubTopicId;
using coin2::exchange::okex::symbology::OkexCurrency;

inline coin::proto::OrderSide GetOrderSide(const JsonValue& data) {
  using coin::proto::OrderSide;
  std::string_view direction = data["side"].GetString();
  if (direction == "buy") {
    return OrderSide::BUY_ORDER;
  } else if (direction == "sell") {
    return OrderSide::SELL_ORDER;
  } else {
    return OrderSide::UNKNOWN_ORDER_SIDE;
  }
}

inline int64_t GetProcOrderId(const JsonValue& data) {
  int64_t proc_order_id = 0;
  if (data.HasMember("clOrdId")) {
    std::string_view proc_str = data["clOrdId"].GetString();
    try {
      proc_order_id = stoll(proc_str.substr(2));
    } catch (std::exception& e) {
      LOG(ERROR) << e.what() << JsonToString(data);
    }
  }
  return proc_order_id;
}

bool IsOkexErrMessage(const JsonValue& doc) {
  if (doc.HasMember("code") && std::string(doc["code"].GetString()) != "0") {
    return true;
  }
  if (doc.HasMember("msg") && std::string(doc["msg"].GetString()) != "") {
    return true;
  }
  return false;
}

bool IsOrderUpdate(const rapidjson::GenericValue<rapidjson::UTF8<>>& json) {
  if (!(json.HasMember("arg") && json["arg"].HasMember("channel") &&
        json["arg"].HasMember("instType"))) {
    return false;
  }

  return std::string(json["arg"]["channel"].GetString()) == "orders" &&
         std::string(json["arg"]["instType"].GetString()) == "SPOT";
}

}  // namespace

void OkexV5Parser::ParseRestAccountBalance() {
  JsonDocument document;
  if (!PrepareJsonDocument(&document)) {
    LOG(ERROR) << "[ParseRestAccountBalance] Parsing Error. " << msg_->DebugString();
    result_.set_error_code(OrderErrorCode::INVALID_JSON);
    return;
  }

  auto account_balance = ParseBalance(document);
  if (!account_balance.has_exchange() || !account_balance.has_market_type()) {
    result_.set_error_code(OrderErrorCode::INVALID_JSON);
    return;
  }
  result_.set_type(MsgType::ACCOUNT_BALANCE);
  *result_.mutable_account_balance() = std::move(account_balance);
}

// NOTE: when field exchange and market_type are set, which means parsed successfully
AccountBalance OkexV5Parser::ParseBalance(const JsonValue& doc) {
  RawBalanceMap raw_balance_map;
  auto success = ParseRawBalance(doc, &raw_balance_map);
  if (!success) {
    return {};
  }

  AccountBalance acc_balance;
  acc_balance.set_exchange("Okex");
  acc_balance.set_market_type("Spot");
  for (const auto& native_currency : symcache_->GetCurrenciesNativeUpper()) {
    auto* balance = acc_balance.add_each_balance();
    if (raw_balance_map.count(native_currency) == 0) {
      balance->set_total(0);
      balance->set_hold(0);
      balance->set_available(0);
    } else {
      balance->MergeFrom(raw_balance_map.at(native_currency));
    }
    auto* currency = symcache_->GetCurrencyByNative(native_currency);
    balance->set_currency(currency->currency());
    balance->set_currency_native(currency->native_currency());
    DLOG(INFO) << "BALANCE: " << currency->native_currency() << " " << balance->total() << " "
               << balance->available();
  }
  is_account_balance_initialized_ = true;

  return acc_balance;
}

bool OkexV5Parser::ParseRawBalance(const JsonValue& document, RawBalanceMap* raw_balance_map) {
  // Account balance
  /*
  {
      "code": "0",
      "msg": "",
      "data": [{
          "uTime": "1597026383085",
          "totalEq": "41624.32",
          "isoEq": "3624.32",
          "adjEq": "41624.32",
          "imr": "4162.33",
          "mmr": "4",
          "mgnRatio": "41624.32",
          "details": [{
                  "ccy": "BTC",
                  "eq": "123",
                  "isoEq":"",
                  "availEq": "1234",
                  "availBal": "",
                  "frozenBal": "14",
                  "upl": "124",
                  "mgnRatio": "",
                  "interest": "12",
                  "liab": "1"
              },
              ...
          ]
      }]
  }
  */
  // Asset balance
  /*
  {
      "code": "0",
      "msg": "",
      "data": [{
              "availBal": "37.11827078",
              "bal": "37.11827078",
              "ccy": "ETH",
              "frozenBal": "0"
          }
      ]
  }
  */
  if (document.IsObject() && IsOkexErrMessage(document)) {
    LOG(ERROR) << "[ParseRawBalance] " << JsonToString(document);
    return false;
  }
  if (!document.HasMember("data") || !document["data"].IsArray()) {
    LOG(ERROR) << "[ParseRawBalance] " << JsonToString(document);
    return false;
  }

  if (document["data"].GetArray().Size() > 0) {
    if (document["data"][0].HasMember("details")) {
      for (const auto& data : document["data"].GetArray()) {
        for (const auto& detail : data["details"].GetArray()) {
          if (!detail.HasMember("ccy") || !detail.HasMember("eq") || !detail.HasMember("availEq")) {
            continue;
          }
          CurrencyBalance balance;
          balance.set_currency_native(detail["ccy"].GetString());
          auto total = stod(detail["eq"].GetString());
          auto avail = stod(detail["availBal"].GetString());
          balance.set_total(total);
          balance.set_hold(total - avail);
          balance.set_available(avail);
          raw_balance_map->emplace(balance.currency_native(), balance);
        }
      }
    } else if (document["data"][0].HasMember("ccy")) {
      for (const auto& data : document["data"].GetArray()) {
        if (!data.HasMember("ccy") || !data.HasMember("bal") || !data.HasMember("availBal")) {
          continue;
        }
        CurrencyBalance balance;
        balance.set_currency_native(data["ccy"].GetString());
        auto total = stod(data["bal"].GetString());
        auto avail = stod(data["availBal"].GetString());
        balance.set_total(total);
        balance.set_hold(total - avail);
        balance.set_available(avail);
        raw_balance_map->emplace(balance.currency_native(), balance);
      }
    }
  }

  return true;
}

void OkexV5Parser::ParseOrderInfo(const JsonValue& data, coin::proto::ProductOrderElement* order) {
  order->set_market_type(MarketType::Spot);
  order->set_exchange_type(ExchangeType::Okex);
  order->set_api_version("v5");
  order->set_exchange_order_id(data["ordId"].GetString());
  order->set_symbol(data["instId"].GetString());
  // no price info for market order
  std::string_view px = data["px"].GetString();
  if (px == "") {
    order->set_price(0);
  } else {
    order->set_price(stod(data["px"].GetString()));
  }
  order->set_qty(stod(data["sz"].GetString()));
  order->set_proc_order_id(GetProcOrderId(data));
  order->set_side(GetOrderSide(data));
  std::string_view type_str = data["ordType"].GetString();
  if (type_str == "market") {
    order->set_order_type(coin::proto::OrderType::MARKET_ORDER);
  } else {
    order->set_order_type(coin::proto::OrderType::LIMIT_ORDER);
  }
  order->set_timestamp(msg_->timestamp);
  const std::string state = data["state"].GetString();
  if (state == "live" || state == "partially_filled") {
    order->set_is_live(true);
  } else if (state == "canceled") {
    order->set_cancel_confirmed(true);
  } else if (state == "filled") {
    order->set_fully_filled(true);
  } else {
    LOG(ERROR) << "Unknown order status! " << state;
  }
}

void OkexV5Parser::ParseRestOpenOrders() {
  /*
  {
    "code":"0",
    "msg":"",
    "data":[
        {
            "instType":"FUTURES",
            "instId":"BTC-USD-200329",
            "ccy":"",
            "ordId":"123445",
            "clOrdId":"b1",
            "tag":"",
            "px":"999",
            "sz":"3",
            "pnl":"5",
            "ordType":"limit",
            "side":"buy",
            "posSide":"long",
            "tdMode":"isolated",
            "accFillSz":"isolated",
            "fillPx":"0",
            "tradeId":"0",
            "fillSz":"0",
            "fillTime":"0",
            "state":"live",
            "avgPx":"0",
            "lever":"20",
            "tpTriggerPx":"",
            "tpOrdPx":"",
            "slTriggerPx":"",
            "slOrdPx":"",
            "feeCcy":"",
            "fee":"",
            "rebateCcy":"",
            "rebate":"",
            "category":"",
            "uTime":"1597026383085",
            "cTime":"1597026383085"
        }
    ]
  }
  */
  JsonDocument document;
  if (!PrepareJsonDocument(&document)) {
    LOG(ERROR) << "[ParseRestOrders] Parsing Error. " << msg_->DebugString();
    result_.set_error_code(OrderErrorCode::INVALID_JSON);
    return;
  }

  auto product_order_bundle = result_.mutable_order_list();
  product_order_bundle->Clear();

  if (document.IsObject() && IsOkexErrMessage(document)) {
    LOG(ERROR) << "[ParseRestOrders] " << JsonToString(document);
    return;
  }
  if (!document.HasMember("data") || !document["data"].IsArray()) {
    LOG(ERROR) << "[ParseRestOrders] " << JsonToString(document);
    return;
  }

  int record_cnt = 0;
  std::string after_order_id = "";
  bool product_order_bundle_found = false;
  for (const auto& info : document["data"].GetArray()) {
    auto native_symbol = info["instId"].GetString();
    if (!symcache_->has_product_native(native_symbol)) {
      continue;
    }
    auto order = product_order_bundle->add_each_order();
    ParseOrderInfo(info, order);
    product_order_bundle_found = true;
    ++record_cnt;
    after_order_id = order->exchange_order_id();
  }
  if (record_cnt >= 100) {
    mb_->PublishEvent("okex_spot/query_open_orders", &after_order_id);
  }
  if (product_order_bundle_found) {
    result_.set_type(MsgType::ORDER_STATUS);
  }
}

void OkexV5Parser::ParseRestHistOrders() {
  /*
  {
    "code":"0",
    "msg":"",
    "data":[
        {
            "instType":"FUTURES",
            "instId":"BTC-USD-200329",
            "ccy":"",
            "ordId":"123445",
            "clOrdId":"b1",
            "tag":"",
            "px":"999",
            "sz":"3",
            "pnl":"5",
            "ordType":"limit",
            "side":"buy",
            "posSide":"long",
            "tdMode":"isolated",
            "accFillSz":"isolated",
            "fillPx":"0",
            "tradeId":"0",
            "fillSz":"0",
            "fillTime":"0",
            "state":"live",
            "avgPx":"0",
            "lever":"20",
            "tpTriggerPx":"",
            "tpOrdPx":"",
            "slTriggerPx":"",
            "slOrdPx":"",
            "feeCcy":"",
            "fee":"",
            "rebateCcy":"",
            "rebate":"",
            "category":"",
            "uTime":"1597026383085",
            "cTime":"1597026383085"
        }
    ]
  }
  */
  JsonDocument document;
  if (!PrepareJsonDocument(&document)) {
    LOG(ERROR) << "[ParseRestOrders] Parsing Error. " << msg_->DebugString();
    result_.set_error_code(OrderErrorCode::INVALID_JSON);
    return;
  }

  auto product_order_bundle = result_.mutable_order_list();
  product_order_bundle->Clear();

  if (document.IsObject() && IsOkexErrMessage(document)) {
    LOG(ERROR) << "[ParseRestOrders] " << JsonToString(document);
    return;
  }
  if (!document.HasMember("data") || !document["data"].IsArray()) {
    LOG(ERROR) << "[ParseRestOrders] " << JsonToString(document);
    return;
  }

  bool product_order_bundle_found = false;
  for (const auto& info : document["data"].GetArray()) {
    auto native_symbol = info["instId"].GetString();
    if (!symcache_->has_product_native(native_symbol)) {
      continue;
    }
    auto order = product_order_bundle->add_each_order();
    ParseOrderInfo(info, order);
    product_order_bundle_found = true;
  }
  if (product_order_bundle_found) {
    result_.set_type(MsgType::ORDER_STATUS);
  }
}

void OkexV5Parser::ParseWsOrderInfoList(const JsonDocument& document) {
  /*
  {
    "arg": {
        "channel": "orders",
        "instType": "FUTURES",
        "instId": "BTC-USD-200329"
    },
    "data": [{
        "instType": "FUTURES",
        "instId": "BTC-USD-200329",
        "ordId": "123445",
        "clOrdId": "b1",
        "tag": "",
        "px": "999",
        "sz": "3",
        "ordType": "limit",
        "side": "buy",
        "posSide": "long",
        "tdMode": "cross",
        "fillSz": "0",
        "fillPx": "long",
        "tradeId": "0",
        "accFillSz": "323",
        "fillTime": "0",
        "state": "canceled",
        "avgPx": "0",
        "lever": "20",
        "tpTriggerPx": "0",
        "tpOrdPx": "20",
        "slTriggerPx": "0",
        "slOrdPx": "20",
        "feeCcy": "",
        "fee": "",
        "rebateCcy": "",
        "rebate": "",
        "pnl": "",
        "category": "",
        "uTime": "1597026383085",
        "cTime": "1597026383085",
        "reqId": "",
        "amendResult": "",
        "code": "0",
        "msg": ""
    }]
  }
  */
  auto product_order_bundle = result_.mutable_order_list();
  product_order_bundle->Clear();

  if (!document.HasMember("data") || !document["data"].IsArray()) {
    LOG(ERROR) << "[ParseWsOrderInfoList] " << JsonToString(document);
    return;
  }

  bool product_order_bundle_found = false;
  for (const auto& info : document["data"].GetArray()) {
    if (IsOkexErrMessage(info)) {
      LOG(ERROR) << "[ParseWsOrderInfoList] " << JsonToString(info);
      continue;
    }
    auto order = product_order_bundle->add_each_order();
    ParseOrderInfo(info, order);
    product_order_bundle_found = true;
  }
  if (product_order_bundle_found) {
    result_.set_type(MsgType::ORDER_STATUS);
  }
}

void OkexV5Parser::ParseFillInfo(const JsonValue& data, coin::proto::ProductFillElement* fill) {
  fill->set_symbol(data["instId"].GetString());
  fill->set_exchange_order_id(data["ordId"].GetString());
  fill->set_price(stod(data["fillPx"].GetString()));
  fill->set_qty(stod(data["fillSz"].GetString()));
  fill->set_fill_id(data["tradeId"].GetString());
  if (data.HasMember("fee")) {
    if (std::string fee = data["fee"].GetString(); !fee.empty()) {
      fill->set_fee(-stod(fee));
    }
  }
  if (data.HasMember("feeCcy")) {
    if (std::string fee_currency = data["feeCcy"].GetString(); !fee_currency.empty()) {
      fill->set_fee_currency(fee_currency);
    }
  }
  if (!data.HasMember("execType")) {
    // Infer fill type logic, for ws msg
    auto oc = ocm_->FindByProcOrderId(fill->proc_order_id());
    if (oc) {
      if (std::abs(fill->price() - oc->order_spec().price) > 1e-6) {
        fill->set_fill_type(FillType::TAKER_FILL_TYPE);
      } else if (oc->order_spec().post_only) {
        fill->set_fill_type(FillType::MAKER_FILL_TYPE);
      } else if (fill->fill_timestamp() - oc->order_created_time() > 5e9) {
        fill->set_fill_type(FillType::MAKER_FILL_TYPE);
      } else {
        fill->set_fill_type(FillType::TAKER_FILL_TYPE);
      }
    } else {
      fill->set_fill_type(FillType::TAKER_FILL_TYPE);
    }
    return;
  }

  std::string_view exec_type_str = data["execType"].GetString();
  if (exec_type_str == "M") {
    fill->set_fill_type(FillType::MAKER_FILL_TYPE);
  } else if (exec_type_str == "T") {
    fill->set_fill_type(FillType::TAKER_FILL_TYPE);
  } else {
    fill->set_fill_type(FillType::UNKNOWN_FILL_TYPE);
  }
}

void OkexV5Parser::ParseRestFillOrders() {
  /*
  {
    "code":"0",
    "msg":"",
    "data":[
         {
            "instType":"FUTURES",
            "instId":"BTC-USD-200329",
            "tradeId":"123",
            "ordId":"123445",
            "billId":"1111",
            "tag":"",
            "fillPx":"999",
            "fillSz":"3",
            "side":"buy",
            "posSide":"long",
            "execType":"M",
            "feeCcy":"",
            "fee":"",
            "ts":"1597026383085"
        },
        ...
    ]
  }
  */
  JsonDocument document;
  if (!PrepareJsonDocument(&document)) {
    LOG(ERROR) << "[ParseRestFillOrders] Parsing Error. " << msg_->DebugString();
    result_.set_error_code(OrderErrorCode::INVALID_JSON);
    return;
  }

  auto product_fill_bundle = result_.mutable_fill_list();
  product_fill_bundle->Clear();

  if (document.IsObject() && IsOkexErrMessage(document)) {
    LOG(ERROR) << "[ParseRestFillOrders] " << JsonToString(document);
    return;
  }

  if (!document.HasMember("data") || !document["data"].IsArray()) {
    LOG(ERROR) << "[ParseRestFillOrders] " << JsonToString(document);
    return;
  }

  bool product_fill_bundle_found = false;
  for (const auto& info : document["data"].GetArray()) {
    auto native_symbol = info["instId"].GetString();
    if (!symcache_->has_product_native(native_symbol)) {
      continue;
    }
    auto fill = product_fill_bundle->add_each_fill();
    ParseFillInfo(info, fill);
    product_fill_bundle_found = true;
  }
  if (product_fill_bundle_found) {
    result_.set_type(MsgType::ORDER_FILL);
  }
}

void OkexV5Parser::ParseWsFillInfoList(const JsonDocument& document) {
  // same as ParseWsOrderInfoList
  auto product_fill_bundle = result_.mutable_fill_list();
  product_fill_bundle->Clear();

  if (!document.HasMember("data") || !document["data"].IsArray()) {
    LOG(ERROR) << "[ParseWsFillInfoList] " << JsonToString(document);
    return;
  }

  bool product_fill_bundle_found = false;
  for (const auto& info : document["data"].GetArray()) {
    if (IsOkexErrMessage(info)) {
      LOG(ERROR) << "[ParseWsFillInfoList] " << JsonToString(info);
      continue;
    }
    const std::string fill_sz = info["fillSz"].GetString();
    if (fill_sz == "" || fill_sz == "0") {
      continue;
    }
    auto fill = product_fill_bundle->add_each_fill();
    ParseFillInfo(info, fill);
    product_fill_bundle_found = true;
  }
  if (product_fill_bundle_found) {
    result_.set_type(MsgType::ORDER_FILL);
  }
}

void OkexV5Parser::ParseSubmitResponse() {
  /*
  {
      "code":"0",
      "msg":"",
      "data":[
          {
              "clOrdId":"oktswap6",
              "ordId":"12345689",
              "tag":"",
              "sCode":"0",
              "sMsg":""
          }
      ]
  }
  */
  const auto order_context = msg_->oc;
  const auto& http_context = msg_->http_context;
  if (!IsValidResponse(http_context)) {
    return;
  }

  result_.set_type(MsgType::SUBMIT_RESPONSE);
  auto resp = result_.mutable_submit_response();
  resp->Clear();
  resp->set_symbol(order_context->order_spec().product);
  resp->set_proc_order_id(order_context->proc_order_id());
  resp->set_http_status(http_context->res.result_int());
  resp->set_timestamp(msg_->timestamp);

  if (http_context->status == AsioHttpContextStatus::kError) {
    resp->set_error_code(coin::proto::OrderErrorCode::ORDER_NETWORK_ERROR);
    resp->set_success(false);
    return;
  }

  if (http_context->res.result_int() == 429) {
    LOG(ERROR) << "[OkexSubmitOrder] API_LIMIT_ERROR";
    resp->set_error_code(coin::proto::OrderErrorCode::API_LIMIT_ERROR);
    resp->set_success(false);
    return;
  }

  JsonDocument document;
  if (!PrepareJsonDocument(&document)) {
    LOG(ERROR) << "[OkexSubmitOrder] Parsing Error." << msg_->DebugString();
    resp->set_error_code(coin::proto::OrderErrorCode::ORDER_API_ERROR);
    resp->set_success(false);
    return;
  }

  if (!document.HasMember("code") || !document.HasMember("msg") || !document.HasMember("data") ||
      !document["data"].IsArray() || document["data"].Size() != 1 ||
      !document["data"][0].HasMember("sCode") || !document["data"][0].HasMember("sMsg") ||
      !document["data"][0].HasMember("ordId")) {
    LOG(ERROR) << "[OkexSubmitOrder] Parsing Error." << msg_->DebugString();
    resp->set_error_code(coin::proto::OrderErrorCode::ORDER_API_ERROR);
    resp->set_success(false);
    return;
  }
  auto error_code = GetOrderErrorCode(document["data"][0]["sCode"].GetString());
  resp->set_error_code(error_code);
  resp->set_exchange_error_code(document["data"][0]["sCode"].GetString());
  resp->set_exchange_error_msg(document["data"][0]["sMsg"].GetString());
  if (error_code != coin::proto::OrderErrorCode::ORDER_NO_ERROR) {
    LOG(ERROR) << JsonToString(document);
    resp->set_success(false);
    return;
  }

  resp->set_success(true);
  resp->set_exchange_order_id(document["data"][0]["ordId"].GetString());
}

void OkexV5Parser::ParseBatchSubmitResponse() {
  /*
  {
      "code":"0",
      "msg":"",
      "data":[
          {
              "clOrdId":"oktswap6",
              "ordId":"12345689",
              "tag":"",
              "sCode":"0",
              "sMsg":""
          },
          {
              "clOrdId":"oktswap7",
              "ordId":"12344",
              "tag":"",
              "sCode":"0",
              "sMsg":""
          }
      ]
  }
  */
  const auto& oc_list = msg_->oc_list;
  const auto& http_context = msg_->http_context;
  if (!IsValidResponse(http_context)) {
    return;
  }

  result_.set_type(MsgType::MULTI_SUBMIT_RESPONSE);
  auto* response = result_.mutable_multi_submit_response();
  response->Clear();

  std::unordered_map<int64_t, SubmitOrderResponse*> proc_order_id_to_response_map;
  for (const auto* order_context : oc_list) {
    auto* resp = response->add_each_submit_response();

    resp->set_symbol(order_context->order_spec().product);
    resp->set_proc_order_id(order_context->proc_order_id());
    resp->set_http_status(http_context->res.result_int());
    resp->set_timestamp(msg_->timestamp);
    resp->set_success(false);

    CHECK(proc_order_id_to_response_map.count(order_context->proc_order_id()) == 0)
        << "proc_order_id duplicate proc_order_id:" << order_context->proc_order_id();
    proc_order_id_to_response_map[order_context->proc_order_id()] = resp;
  }

  if (http_context->status == AsioHttpContextStatus::kError) {
    LOG(ERROR) << "[" << __func__ << "] AsioHttpContextStatus Error";
    for (auto& resp : *(response->mutable_each_submit_response())) {
      resp.set_error_code(coin::proto::OrderErrorCode::ORDER_NETWORK_ERROR);
    }
    return;
  }

  if (http_context->res.result_int() == 429) {
    LOG(ERROR) << "[" << __func__ << "] API_LIMIT_ERROR";
    for (auto& resp : *(response->mutable_each_submit_response())) {
      resp.set_error_code(coin::proto::OrderErrorCode::API_LIMIT_ERROR);
    }
    return;
  }

  JsonDocument json;
  if (!PrepareJsonDocument(&json) || !json.IsObject() || !json.HasMember("data") ||
      !json["data"].IsArray()) {
    LOG(ERROR) << "[" << __func__ << "] Parse Error\n" << http_context->res;
    for (auto& resp : *(response->mutable_each_submit_response())) {
      resp.set_error_code(coin::proto::OrderErrorCode::ORDER_API_ERROR);
    }
    return;
  }

  const auto& array = json["data"].GetArray();

  CHECK_EQ(proc_order_id_to_response_map.size(), array.Size())
      << "Batch submit response size not equal to request size response:" << JsonToString(json)
      << " request:" << response->DebugString();

  for (const auto& data : array) {
    int64_t client_order_id = GetProcOrderId(data);

    CHECK(proc_order_id_to_response_map.count(client_order_id) != 0)
        << "unknown client order id found client_order_id:" << client_order_id;

    auto* resp = proc_order_id_to_response_map[client_order_id];

    auto error_code = GetOrderErrorCode(data["sCode"].GetString());
    resp->set_error_code(error_code);
    resp->set_exchange_error_code(data["sCode"].GetString());
    resp->set_exchange_error_msg(data["sMsg"].GetString());
    if (error_code != coin::proto::OrderErrorCode::ORDER_NO_ERROR) {
      LOG(ERROR) << JsonToString(data);
      continue;
    }

    resp->set_success(true);
    resp->set_exchange_order_id(data["ordId"].GetString());
  }
}

void OkexV5Parser::ParseCancelResponse() {
  // same as ParseSubmitResponse
  const auto order_context = msg_->oc;
  const auto& http_context = msg_->http_context;
  if (!IsValidResponse(http_context)) {
    return;
  }

  result_.set_type(MsgType::CANCEL_RESPONSE);
  auto resp = result_.mutable_cancel_response();
  resp->set_symbol(order_context->order_spec().product);
  resp->set_proc_order_id(order_context->proc_order_id());
  resp->set_http_status(http_context->res.result_int());
  resp->set_exchange_order_id(order_context->exchange_order_id());
  resp->set_timestamp(msg_->timestamp);

  if (http_context->status == AsioHttpContextStatus::kError) {
    resp->set_error_code(coin::proto::OrderErrorCode::ORDER_NETWORK_ERROR);
    resp->set_success(false);
    return;
  }

  if (http_context->res.result_int() == 429) {
    LOG(ERROR) << "[OkexCancelOrder] API_LIMIT_ERROR";
    resp->set_error_code(coin::proto::OrderErrorCode::API_LIMIT_ERROR);
    resp->set_success(false);
    return;
  }

  JsonDocument document;
  if (!PrepareJsonDocument(&document)) {
    LOG(ERROR) << "[OkexCancelOrder] Parsing Error." << msg_->DebugString();
    resp->set_error_code(coin::proto::OrderErrorCode::ORDER_API_ERROR);
    resp->set_success(false);
    return;
  }

  if (!document.HasMember("code") || !document.HasMember("msg") || !document.HasMember("data") ||
      !document["data"].IsArray() || document["data"].Size() != 1 ||
      !document["data"][0].HasMember("sCode") || !document["data"][0].HasMember("sMsg") ||
      !document["data"][0].HasMember("ordId")) {
    LOG(ERROR) << "[OkexCancelOrder] Parsing Error." << msg_->DebugString();
    resp->set_error_code(coin::proto::OrderErrorCode::ORDER_API_ERROR);
    resp->set_success(false);
    return;
  }

  auto error_code = GetOrderErrorCode(document["data"][0]["sCode"].GetString());
  resp->set_error_code(error_code);
  resp->set_exchange_error_code(document["data"][0]["sCode"].GetString());
  resp->set_exchange_error_msg(document["data"][0]["sMsg"].GetString());
  if (error_code != coin::proto::OrderErrorCode::ORDER_NO_ERROR) {
    LOG(ERROR) << "Cancel failed! " << JsonToString(document) << ", "
               << order_context->exchange_order_id();
    resp->set_success(false);
    return;
  }

  resp->set_exchange_order_id(std::string(document["data"][0]["ordId"].GetString()));
  resp->set_success(true);
}

void OkexV5Parser::ParseBatchCancelResponse() {
  /*
  {
      "code":"0",
      "msg":"",
      "data":[
          {
              "clOrdId":"oktswap6",
              "ordId":"12345689",
              "sCode":"0",
              "sMsg":""
          },
          {
              "clOrdId":"oktswap7",
              "ordId":"12344",
              "sCode":"0",
              "sMsg":""
          }
      ]
  }
  */

  const auto& oc_list = msg_->oc_list;
  const auto& http_context = msg_->http_context;
  if (!IsValidResponse(http_context)) {
    return;
  }

  result_.set_type(MsgType::MULTI_CANCEL_RESPONSE);
  auto* response = result_.mutable_multi_cancel_response();
  response->Clear();

  std::unordered_map<int64_t, CancelOrderResponse*> proc_order_id_to_response_map;
  std::unordered_map<std::string, CancelOrderResponse*> order_id_to_response_map;
  for (const auto* order_context : oc_list) {
    auto* resp = response->add_each_cancel_response();

    resp->set_symbol(order_context->order_spec().product);
    resp->set_proc_order_id(order_context->proc_order_id());
    resp->set_http_status(http_context->res.result_int());
    resp->set_exchange_order_id(order_context->exchange_order_id());
    resp->set_timestamp(msg_->timestamp);
    resp->set_success(false);

    CHECK(proc_order_id_to_response_map.count(order_context->proc_order_id()) == 0)
        << "proc_order_id duplicate proc_order_id:" << order_context->proc_order_id();
    proc_order_id_to_response_map[order_context->proc_order_id()] = resp;

    if (order_context->has_exchange_order_id()) {
      CHECK(order_id_to_response_map.count(order_context->exchange_order_id()) == 0)
          << "exchange_order_id duplicate exchange_order_id:" << order_context->exchange_order_id();
      order_id_to_response_map[order_context->exchange_order_id()] = resp;
    }
  }

  if (http_context->status == AsioHttpContextStatus::kError) {
    LOG(ERROR) << "[" << __func__ << "] AsioHttpContextStatus Error";
    for (auto& resp : *(response->mutable_each_cancel_response())) {
      resp.set_error_code(coin::proto::OrderErrorCode::ORDER_NETWORK_ERROR);
    }
    return;
  }

  if (http_context->res.result_int() == 429) {
    LOG(ERROR) << "[" << __func__ << "] API_LIMIT_ERROR";
    for (auto& resp : *(response->mutable_each_cancel_response())) {
      resp.set_error_code(coin::proto::OrderErrorCode::API_LIMIT_ERROR);
    }
    return;
  }

  JsonDocument json;
  if (!PrepareJsonDocument(&json) || !json.IsObject() || !json.HasMember("data") ||
      !json["data"].IsArray()) {
    LOG(ERROR) << "[" << __func__ << "] Parse Error\n" << http_context->res;
    for (auto& resp : *(response->mutable_each_cancel_response())) {
      resp.set_error_code(coin::proto::OrderErrorCode::ORDER_API_ERROR);
    }
    return;
  }

  const auto& array = json["data"].GetArray();

  CHECK_EQ(proc_order_id_to_response_map.size(), array.Size())
      << "Batch cancel response size not equal to request size msg:" << msg_->DebugString();

  for (const auto& data : array) {
    int64_t client_order_id = GetProcOrderId(data);
    std::string order_id = data["ordId"].GetString();

    CancelOrderResponse* resp;
    if (proc_order_id_to_response_map.count(client_order_id) != 0) {
      resp = proc_order_id_to_response_map[client_order_id];
    } else if (order_id_to_response_map.count(order_id) != 0) {
      resp = order_id_to_response_map[order_id];
    } else {
      NOTREACHED() << "response: " << JsonToString(data)
                   << ", can not find the CancelOrderResponse for this cancel response!";
    }

    auto error_code = GetOrderErrorCode(data["sCode"].GetString());
    resp->set_error_code(error_code);
    resp->set_exchange_error_code(data["sCode"].GetString());
    resp->set_exchange_error_msg(data["sMsg"].GetString());
    if (error_code != coin::proto::OrderErrorCode::ORDER_NO_ERROR) {
      LOG(ERROR) << "Cancel failed! " << JsonToString(data) << ", " << resp->exchange_order_id();
      continue;
    }

    resp->set_success(true);
    resp->set_exchange_order_id(order_id);
  }
}

void OkexV5Parser::ParseRestDeposit() {
  /*
  {
    "code": "0",
    "data": [{
      "amt": "100000",
      "ccy": "USDT",
      "chain": "USDT-TRC20",
      "depId": "30556253",
      "from": "",
      "state": "2",
      "to": "TX5krATmLzJ69iJB9tgf7uVWoJiisMuWJR",
      "ts": "1635254454000",
      "txId": "a35d536f637729f20ffe740d0324b7dfd156d38ee43d418b04f80d14b42adc5b"
    }],
    "msg": ""
  }
  */
  JsonDocument document;
  if (!PrepareJsonDocument(&document)) {
    LOG(ERROR) << "[ParseRestDeposit] Parsing error\n" << msg_->DebugString();
    result_.set_error_code(OrderErrorCode::INVALID_JSON);
    return;
  }

  if (document.IsObject() && IsOkexErrMessage(document)) {
    LOG(ERROR) << "[ParseRestDeposit] Error message\n" << JsonToString(document);
    result_.set_error_code(OrderErrorCode::INVALID_MSG_TYPE);
    return;
  }

  if (!document.HasMember("data") || !document["data"].IsArray()) {
    LOG(ERROR) << "[ParseRestDeposit] Invalid data format\n" << JsonToString(document);
    result_.set_error_code(OrderErrorCode::INVALID_MSG_TYPE);
    return;
  }

  auto currency_transfer_bundle = result_.mutable_currency_transfer_bundle();
  currency_transfer_bundle->Clear();
  bool currency_transfer_bundle_found = false;
  for (const auto& data : document["data"].GetArray()) {
    if (!data.HasMember("ccy") || !data.HasMember("amt")) {
      LOG(ERROR) << "[ParseRestDeposit] Invalid transfer format" << JsonToString(data);
      continue;
    }
    std::string transfer_id;
    if (!data.HasMember("txId") || data["txId"].IsNull() ||
        (std::string(data["txId"].GetString()) == "")) {
      if (!data.HasMember("depId")) {
        LOG(ERROR) << "[ParseRestDeposit] Invalid transfer format" << JsonToString(data);
        continue;
      }
      transfer_id = data["depId"].GetString();
    } else {
      transfer_id = data["txId"].GetString();
    }

    if (transfer_deposit_map_.count(transfer_id) <= 0) {
      transfer_deposit_map_[transfer_id] = "";
    }
    std::string state = data["state"].GetString();
    if ((state != "0") && (state != "1") && (state != "2") && (state != "8")) {
      continue;
    }

    if (!transfer_first_record_) {
      if (state == transfer_deposit_map_[transfer_id]) {
        continue;
      }
    }

    transfer_deposit_map_[transfer_id] = state;

    int64_t create_time = stoll(data["ts"].GetString()) * 1e6;
    auto currency = OkexCurrency::FromNativeStrImpl(data["ccy"].GetString(), create_time);
    auto* currency_transfer = currency_transfer_bundle->add_each_transfer();
    currency_transfer->set_currency(currency->currency());
    currency_transfer->set_type(TransferRequestProto::DEPOSIT);
    if ((state == "0") || (state == "8")) {
      currency_transfer->set_status(TransferRequestProto::TRANSFER_SUBMITTED);
    } else if ((state == "1") || (state == "2")) {
      currency_transfer->set_status(TransferRequestProto::TRANSFER_CONFIRMED);
    }
    currency_transfer->set_external_transfer_id(transfer_id);
    currency_transfer->set_qty(stod(data["amt"].GetString()));
    currency_transfer->set_from_address(data["from"].GetString());
    currency_transfer->set_to_address(data["to"].GetString());
    currency_transfer->set_create_timestamp(create_time);
    currency_transfer_bundle_found = true;
  }

  if (!is_transfer_deposit_initialized_) {
    is_transfer_deposit_initialized_ = true;
    LOG(INFO) << "[ParseRestDeposit] Deposit ignored: " << transfer_deposit_map_.size();
    return;
  }

  if (!transfer_first_record_) {
    if (currency_transfer_bundle_found) {
      LOG(INFO) << "[ParseRestDeposit] Deposit parsed: "
                << currency_transfer_bundle->each_transfer_size();
      result_.set_type(MsgType::TRANSFER);
    }
  }
}

void OkexV5Parser::ParseRestTransfer() {
  /*  {
    "code": "0",
    "data": [
      {
        "amt": "100000",
        "billId": "24276572",
        "ccy": "USDT",
        "subAcct": "prestoinvt07",
        "ts": "1665202390000",
        "type": "0"
      },
      {
        "amt": "0.00000001",
        "billId": "24276561",
        "ccy": "USDT",
        "subAcct": "prestoinvt07",
        "ts": "1665202382000",
        "type": "1"
      }
    ],
    "msg": ""
  }
  */

  JsonDocument document;
  if (!PrepareJsonDocument(&document)) {
    LOG(ERROR) << "[ParseRestTransfer] Parsing error\n" << msg_->DebugString();
    result_.set_error_code(OrderErrorCode::INVALID_JSON);
    return;
  }

  if (document.IsObject() && IsOkexErrMessage(document)) {
    LOG(ERROR) << "[ParseRestTransfer] Error message\n" << JsonToString(document);
    result_.set_error_code(OrderErrorCode::INVALID_MSG_TYPE);
    return;
  }

  if (!document.HasMember("data") || !document["data"].IsArray()) {
    LOG(ERROR) << "[ParseRestTransfer] Invalid data format\n" << JsonToString(document);
    result_.set_error_code(OrderErrorCode::INVALID_MSG_TYPE);
    return;
  }

  auto currency_transfer_bundle = result_.mutable_currency_transfer_bundle();
  currency_transfer_bundle->Clear();
  bool currency_transfer_bundle_found = false;
  for (const auto& data : document["data"].GetArray()) {
    static std::vector<std::string> fields =
        {"amt", "billId", "ts", "ccy", "type"};  // BillId indicates the user-defined ID
    if (!JsonFieldCheck(data, fields)) {
      LOG(ERROR) << "[ParseRestTransfer] Invalid Deposit format" << JsonToString(data);
      continue;
    }

    std::string key = std::string(data["ts"].GetString()) + std::string(data["billId"].GetString());
    if (!transfer_first_record_) {
      if (transfer_record_map_[key] == "done") {
        continue;
      }
    }

    transfer_record_map_.insert_or_assign(key, "done");
    std::string to_address("prestoinv0"), from_address("prestoinv0");
    auto proto_type = TransferRequestProto::DEPOSIT_SUBACCOUNT;
    if (data["type"].GetString() == std::string("0")) {  //  0 Transfer to Sub account
      proto_type = TransferRequestProto::WITHDRAW_SUBACCOUNT;
      to_address = data["subAcct"].GetString();
    } else if (data["type"].GetString() == std::string("1")) {  // 1 Transfer from Sub account
      proto_type = TransferRequestProto::DEPOSIT_SUBACCOUNT;
      from_address = data["subAcct"].GetString();
    } else {
      LOG(ERROR) << "[ParseRestTransfer] Invalid type" << JsonToString(data);
      continue;
    }

    int64_t create_time = std::stoll(data["ts"].GetString()) * 1e6;
    auto currency = OkexCurrency::FromNativeStrImpl(data["ccy"].GetString(), create_time);
    auto* currency_transfer = currency_transfer_bundle->add_each_transfer();
    currency_transfer->set_type(proto_type);
    currency_transfer->set_from_address(from_address.c_str());
    currency_transfer->set_to_address(to_address.c_str());
    currency_transfer->set_status(TransferRequestProto::TRANSFER_CONFIRMED);
    currency_transfer->set_currency(currency->currency());
    currency_transfer->set_create_timestamp(create_time);
    currency_transfer->set_qty(std::abs(stod(data["amt"].GetString())));
    currency_transfer->set_external_transfer_id(data["billId"].GetString());

    currency_transfer_bundle_found = true;
  }

  if (!transfer_first_record_) {
    if (!is_transfer_initialized_) {
      is_transfer_initialized_ = true;
      LOG(INFO) << "[ParseRestTransfer] Transfer record ignored: " << transfer_record_map_.size();
      return;
    }
  }

  if (currency_transfer_bundle_found) {
    result_.set_type(BaseParser::MsgType::TRANSFER);
  }
}

void OkexV5Parser::ParseRestWithdrawal() {
  /*
  {
    "code": "0",
    "data": [{
      "chain": "BTC-Bitcoin",
      "fee": "0.0004",
      "ccy": "BTC",
      "amt": "4",
      "txId": "929b5f91beb710b4e1974fec5e1e031b3ff2d9e5b3bd1ed4baab1f810ed029aa",
      "from": "87338117",
      "to": "1NtxcSC6XfpUXkbUbCHqa15UYv4nzJUNTL",
      "state": "2",
      "ts": "1621838350000",
      "wdId": "15200565"
    }],
    "msg": ""
  }
  */
  JsonDocument document;
  if (!PrepareJsonDocument(&document)) {
    LOG(ERROR) << "[ParseRestWithdrawal] Parsing error\n" << msg_->DebugString();
    result_.set_error_code(OrderErrorCode::INVALID_JSON);
    return;
  }

  if (document.IsObject() && IsOkexErrMessage(document)) {
    LOG(ERROR) << "[ParseRestWithdrawal] Error message\n" << JsonToString(document);
    result_.set_error_code(OrderErrorCode::INVALID_MSG_TYPE);
    return;
  }

  if (!document.HasMember("data") || !document["data"].IsArray()) {
    LOG(ERROR) << "[ParseRestWithdrawal] Invalid data format\n" << JsonToString(document);
    result_.set_error_code(OrderErrorCode::INVALID_MSG_TYPE);
    return;
  }

  auto currency_transfer_bundle = result_.mutable_currency_transfer_bundle();
  currency_transfer_bundle->Clear();
  bool currency_transfer_bundle_found = false;
  for (const auto& data : document["data"].GetArray()) {
    if (!data.HasMember("ccy") || !data.HasMember("amt")) {
      LOG(ERROR) << "[ParseRestWithdrawal] Invalid transfer format";
      continue;
    }

    std::string transfer_id;
    if (!data.HasMember("txId") || data["txId"].IsNull() ||
        (std::string(data["txId"].GetString()) ==
         "")) {  // If txId does not exist, wdId is used instead
      if (!data.HasMember("wdId")) {
        LOG(ERROR) << "[ParseRestWithdrawal] Invalid transfer format" << JsonToString(document);
        continue;
      }
      transfer_id = data["wdId"].GetString();
    } else {
      transfer_id = data["txId"].GetString();
    }

    if (!transfer_first_record_) {
      if (transfer_withdrawal_map_.count(transfer_id) <= 0) {
        transfer_withdrawal_map_[transfer_id] = "";
      }
    }

    std::string state = data["state"].GetString();
    if ((state != "-3") && (state != "-2") && (state != "-1") && (state != "0") && (state != "1") &&
        (state != "2") && (state != "3") && (state != "4") && (state != "5")) {
      continue;
    }
    if (state == transfer_withdrawal_map_[transfer_id]) {
      continue;
    }
    transfer_withdrawal_map_[transfer_id] = state;

    int64_t create_time = stoll(data["ts"].GetString()) * 1e6;
    auto currency = OkexCurrency::FromNativeStrImpl(data["ccy"].GetString(), create_time);
    auto* currency_transfer = currency_transfer_bundle->add_each_transfer();
    currency_transfer->set_currency(currency->currency());
    currency_transfer->set_type(TransferRequestProto::WITHDRAW);
    if (state == "-3") {
      currency_transfer->set_status(TransferRequestProto::CANCEL_SUBMITTED);
    } else if (state == "-2") {
      currency_transfer->set_status(TransferRequestProto::CANCEL_CONFIRMED);
    } else if (state == "-1") {
      currency_transfer->set_status(TransferRequestProto::TRANSFER_REJECTED);
    } else if (
        (state == "0") || (state == "1") || (state == "3") || (state == "4") || (state == "5")) {
      currency_transfer->set_status(TransferRequestProto::TRANSFER_SUBMITTED);
    } else if (state == "2") {
      currency_transfer->set_status(TransferRequestProto::TRANSFER_CONFIRMED);
    }
    currency_transfer->set_external_transfer_id(transfer_id);
    currency_transfer->set_qty(stod(data["amt"].GetString()));
    currency_transfer->set_from_address(data["from"].GetString());
    currency_transfer->set_to_address(data["to"].GetString());
    currency_transfer->set_create_timestamp(create_time);
    currency_transfer_bundle_found = true;
  }

  if (!transfer_first_record_) {
    if (!is_transfer_withdrawal_initialized_) {
      is_transfer_withdrawal_initialized_ = true;
      LOG(INFO) << "[ParseRestWithdrawal] Withdrawal ignored: " << transfer_withdrawal_map_.size();
      return;
    }
  }

  if (currency_transfer_bundle_found) {
    LOG(INFO) << "[ParseRestWithdrawal] Withdrawal parsed: "
              << currency_transfer_bundle->each_transfer_size();
    result_.set_type(MsgType::TRANSFER);
  }
}

void OkexV5Parser::ParseWsMessage() {
  JsonDocument document;
  if (!PrepareJsonDocument(&document)) {
    LOG(ERROR) << "[ParseWsMessage] Parsing Error. " << msg_->DebugString();
    result_.set_error_code(OrderErrorCode::INVALID_JSON);
    return;
  }

  DLOG(INFO) << "OG ws msg: " << JsonToString(document);
  if (IsOrderUpdate(document)) {
    ParseWsFillInfoList(document);
    ParseWsOrderInfoList(document);
  }
}

auto OkexV5Parser::ParseRawMessage(const RawMessage& msg) -> const ParseResult& {
  result_.Clear();
  msg_ = &msg;
  sub_topic_id_ = static_cast<OrderSubTopicId>(msg.type);

  if (msg.type == OrderSubTopicId::WS_MESSAGE) {
    ParseWsMessage();
    return result_;
  }

  switch (msg.http_context->query_type) {
    case OrderSubTopicId::SUBMIT_RESPONSE:
      ParseSubmitResponse();
      break;
    case OrderSubTopicId::CANCEL_RESPONSE:
      ParseCancelResponse();
      break;
    case OrderSubTopicId::MULTI_SUBMIT_RESPONSE:
      ParseBatchSubmitResponse();
      break;
    case OrderSubTopicId::MULTI_CANCEL_RESPONSE:
      ParseBatchCancelResponse();
      break;
    case OrderSubTopicId::REST_OPEN_ORDERS:
      ParseRestOpenOrders();
      break;
    case OrderSubTopicId::REST_DONE_ORDERS:
      ParseRestHistOrders();
      break;
    case OrderSubTopicId::REST_FILL_ORDERS:
      ParseRestFillOrders();
      break;
    case OrderSubTopicId::REST_ACCOUNT_BALANCE:
      ParseRestAccountBalance();
      break;
    case OkexV5OrderSubTopicId::REST_DEPOSIT:
      ParseRestDeposit();
      break;
    case OkexV5OrderSubTopicId::REST_WITHDRAWAL:
      ParseRestWithdrawal();
      break;
    case OrderSubTopicId::TRANSFER:
      ParseRestTransfer();
      break;
    default:
      LOG(ERROR) << "Parsing Error. " << msg_->DebugString();
      break;
  }

  return result_;
}

}  // namespace coin2::exchange::okex_v5::order_v5
